package org.vidi.amqp.basic;

import com.rabbitmq.client.*;
import org.vidi.amqp.common.ConnectionUtils;
import org.vidi.amqp.common.Constants;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author vidi
 */
public class BasicConsumer {
    public static void main(String[] args) {
        try (Connection connection = ConnectionUtils.newSingleConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(Constants.BASIC_QUEUE_NAME, false, false, false, null);
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println(new String(body, StandardCharsets.UTF_8));
                }
            };
            channel.basicConsume(Constants.BASIC_QUEUE_NAME, true, consumer);
        } catch (TimeoutException | IOException e) {
            e.printStackTrace();
        }
    }
}
